#include"Task.hpp"
#include<iostream>
#include<string>
#include <unistd.h>
#include<vector>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
const int process_number=5;
vector<task_t> tasks;
struct channel
{
    int cmdfd;
    pid_t slaverfd;
    string process_name;
    channel(int _cmdfd,pid_t _slaverfd, const string& _process_name):
    cmdfd(_cmdfd),slaverfd(_slaverfd),process_name(_process_name)
    {}
    /* data */
};

void slaver(int rfd)
{
   
    while(true)
    {
        int comcode;
        int n=read(rfd,&comcode,sizeof(int));
        //cout<<n<<endl;
        if(n==sizeof(int))
        {
            //cout<<"slaver say"<<"<"<<getpid()<<"@ get comcode"<<": comcode"<<comcode<<endl;
            if(comcode>=0&&comcode<tasks.size())
            {
                cout<<getpid()<<"执行";
                tasks[comcode]();
            }

        }
        else
        {
            close(0);
            break;
        }
        sleep(1);
    }
  


}

void initProcessPool(vector<channel>* channels)
{
   
    int pipes[2];
    //第一步创建进程
    for(int i=0;i<process_number;i++)
    {
        int ret=pipe(pipes);
        pid_t id=fork();
        if(id==0)
        {
            close(pipes[1]);
            dup2(pipes[0],0);//统一重定向
            close(pipes[0]);
            slaver(0);
            cout<<"process:"<<getpid()<<"quit"<<endl;
            exit(0);
            //子进程

        }
        else
        {
            close(pipes[0]);
            channels->push_back(channel(pipes[1],id,string("process"+to_string(i))));
            //父进程
           
        }
    }


}

void Debug(vector<channel>* channels)
{
     for(auto item : *channels)
    {
        cout<<item.process_name<<"#"<<item.slaverfd<<"---------"<<item.cmdfd<<endl;
    }

}

void menu()
{
    cout<<"######################################"<<endl;
    cout<<"# 1.刷新日志             2.刷新野怪###"<<endl;
    cout<<"# 3.刷新UI               4.刷新商店###"<<endl;
    cout<<"# 5.检查游戏完整性        0.退出   ###"<<endl;
    cout<<"######################################"<<endl;

}

void ctr_slaver(vector<channel>& channels)
{
    ////////轮询
    // int selected=0;
    // for(int i=0;i<100;i++)
    // {
    //     int comcode=rand()%tasks.size();
    //     if(selected>=channels.size())
    //     selected=0;
    //     cout<<"father give child<"<<channels[selected].process_name<<">#"<<channels[selected].slaverfd<<":cmdcode"<<comcode<<endl;
    //     write(channels[selected].cmdfd,&comcode,sizeof(int));
    //     sleep(1);
    //     selected++;
    // }
    /////////
    int selected=0;
    for(int i=0;i<100;i++)
    {
        int comcode=0;
        menu();
        cin>>comcode;
        comcode-=1;
        if(comcode<0)
        {
            cout<<"退出了"<<endl;
            break;
        }
        if(selected>=channels.size())
        selected=0;
        cout<<"father give child<"<<channels[selected].process_name<<">#"<<channels[selected].slaverfd<<":cmdcode"<<comcode<<endl;
        write(channels[selected].cmdfd,&comcode,sizeof(int));
        sleep(1);
        selected++;
    }

}

void QuitProcess(vector<channel>& channels)
{
    cout<<"进来了"<<endl;
    for(const auto& item : channels){close(item.cmdfd);}
    sleep(10);
    for(const auto& item: channels){waitpid(item.slaverfd,nullptr,0);}
    sleep(5);
}

int main()
{
    srand(time(nullptr)^1024);
    LoadTask(&tasks);
    vector<channel> channels;
    initProcessPool(&channels);
    Debug(&channels);
    ctr_slaver(channels);
    QuitProcess(channels);
    //sleep(1000);
    return 0;
}